package cz.semjobKB.extract.api.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

@Component
public class PostProcessing {
	
	private static Logger logger = Logger.getLogger(PostProcessing.class);
	
	public String removeEndings(String text) {
		
		logger.info("Preprocessing text.");
		
		String[] lines = text.split("\n");
		List<String> newLines = new ArrayList<String>();		
		for (int possition = 0; possition < lines.length; possition++) {
			lines[possition] = lines[possition].trim();
		}
		boolean isLastSentence = true;
		for (int possition = 0; possition < lines.length; possition++) {			
			if (!lines[possition].endsWith(".")) {
				
				if (possition + 1 < lines.length && lines[possition + 1].length() > 0 && lines[possition + 1].charAt(0) >= 'a' && lines[possition + 1].charAt(0) <= 'z') {
					if(!isLastSentence) {
						newLines.add(lines[possition] + " " + lines[possition + 1]);
						isLastSentence = false;
					} else {
						newLines.get(newLines.size() - 1).concat(" " + lines[possition + 1]);
					}
				} else {
					newLines.add(lines[possition]);
					isLastSentence = true;
				}
			} else {
				newLines.add(lines[possition]);
				isLastSentence = true;
			}
		}
		String result = "";
		for (String line : newLines) {
			result += line + "\n";
		}
		return result;
		
	}
	
	String remove(String text) {
		return text;
		
	}
}
